<?php

$oReq =& App::getRequest();
$oTpl =& App::getTemplate();
$oDb =& Database::get();

$iPage = $oReq->get('page', 1);

function metaRefresh($sType, $iPage, $iOffset, $iPageSize, $iCnt)
{
	$sStr = App::format('Mod_Admin.counter_update', array($iOffset, $iCnt));
	$sStr .= '<meta http-equiv="refresh" content="2;url='. App::makeUrl('admin.counters', array('type' => $sType, 'page' => ($iPage+1), 'go' => 'true', 'total' => $iPageSize, 'run' =>'true')) .'">';
	return $sStr;
}

if ($oReq->get('go'))
{
	switch($oReq->get('type'))
	{
		case 'gallery_comments':
			
			$iPageSize = $oReq->get('total');

    	    $iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('main') ."");

			$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

			$aRows = $oDb->getRows("
				SELECT u.id, COUNT(f.cid) AS cnt
				FROM ". App::getT('main') ." AS u
					LEFT JOIN ". App::getT('comments') ." AS f ON(f.typeid = '3' AND f.itemid = u.id)
				GROUP BY u.id
				ORDER BY u.id ASC LIMIT ". $iOffset .",". $iPageSize ."
			");

			foreach ($aRows as $aRow)
			{
				$oDb->update(App::getT('main'), array('comment' => $aRow['cnt']), "id = '". $aRow['id'] ."'");
			}

			if (($iOffset+$iPageSize) < $iCnt)
			{
				$sContent = metaRefresh($oReq->get('type'), $iPage, $iOffset, $iPageSize,  $iCnt);
			}
			else
			{
				$oDb->query("OPTIMIZE TABLE `". App::getT('main') ."`");
				$oTpl->assign(array(
					'done' => true
				));
			}						
		
			break;
		
		case 'user-totals':

			if ($sUser = $oReq->get('user_name'))
			{
 				$aRow = $oDb->getRow("
					SELECT
				 		u.id AS main_user_id,
						u.user AS user_name,
						t.user AS total_user,
						t.total AS temp_total,
						t.viewing AS temp_viewing,
						t.invite AS temp_invites,
						(SELECT COUNT(*) FROM " . App::getT('main') . " AS gallery  WHERE gallery.user_id = u.id) AS total_images,
						(SELECT COUNT(*) FROM " . App::getT('journal') . " AS journal WHERE journal.user_id = u.id) AS total_blogs,
						(SELECT COUNT(*) FROM " . App::getT('polls') . " AS poll WHERE poll.user = u.user) AS total_polls,
						(SELECT COUNT(*) FROM " . App::getT('hot_quiz') . " AS quiz WHERE quiz.user = u.user) AS total_quizzes,
						(SELECT COUNT(*) FROM " . App::getT('comments') . " AS comments WHERE comments.userid = u.id) AS total_comments,
						(SELECT COUNT(*) FROM ". App::getT('forum_topic') ." WHERE user = u.user) AS total_threads,
						(SELECT COUNT(*) FROM ". App::getT('forum_reply') ." WHERE user = u.user) AS total_forum_replies,
						(SELECT COUNT(*) FROM " . App::getT('videos_comments') . " AS video_comments WHERE video_comments.cm_userid = u.id) AS total_video_comments,
						(SELECT COUNT(*) FROM " . App::getT('music_song') . " AS music WHERE music.user_id = u.id) AS total_songs,
						(SELECT COUNT(*) FROM " . App::getT('invite') . " AS invite WHERE invite.user = u.user) AS total_invites,
						(SELECT COUNT(*) FROM ". App::getT('videos') ." AS video WHERE video.vid_userid = u.id) AS total_videos
					FROM ". App::getT('user') ." AS u
						LEFT JOIN " . App::getT('user_info') . " AS t ON(u.user = t.user)
					WHERE u.user = '" . $sUser . "'
					HAVING (total_images + total_blogs + total_polls + total_quizzes + total_comments + total_threads + total_forum_replies + total_video_comments + total_songs + total_invites + total_videos) > 0
				");

 				if (isset($aRow['user_name']))
 				{
					$aRow['final_total'] = ($aRow['total_images'] + $aRow['total_blogs'] + $aRow['total_polls'] + $aRow['total_quizzes'] + $aRow['total_comments'] + $aRow['total_video_comments'] + $aRow['total_threads'] + $aRow['total_forum_replies'] + $aRow['total_songs'] + $aRow['total_invites'] + $aRow['total_videos']);
					if (!$aRow['total_user'])
					{
						echo "Invalid user_info: " . $aRow['user_name'];
						$iNewInsertId = $oDb->insert(App::getT('user_info'), array('user' => $aRow['user_name']));
						echo " -> New ID: " . $iNewInsertId . " <br />";
					}

					$aUpdate = array
					(
						'total' => $aRow['final_total'],
						'gallery' => $aRow['total_images'],
						'journal' => $aRow['total_blogs'],
						'poll' => $aRow['total_polls'],
						'quiz' => $aRow['total_quizzes'],
						'comment' => ($aRow['total_comments'] + $aRow['total_video_comments']),
						'forum' => ($aRow['total_threads'] + $aRow['total_forum_replies']),
						'invite' => $aRow['total_invites'],
						'music' => $aRow['total_songs'],
						'video' => $aRow['total_videos']
					);
					$oDb->update(App::getT('user_info'), $aUpdate, "user = '". $aRow['user_name'] ."'");

					$oTpl->assign(array(
						'done' => true
					));
 				}
			}

			break;

		case 'friends':

			$iPageSize = $oReq->get('total');

    	    $iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('user') ."");

			$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

			$aRows = $oDb->getRows("
				SELECT u.user, COUNT(f.user) AS cnt
				FROM ". App::getT('user') ." AS u
					LEFT JOIN ". App::getT('friends') ." AS f ON(u.user = f.user)
				GROUP BY u.user
				ORDER BY u.id ASC LIMIT ". $iOffset .",". $iPageSize ."
			");

			foreach ($aRows as $aRow)
			{
				$oDb->update(App::getT('user'), array('c_friends' => $aRow['cnt']), "user = '". $aRow['user'] ."'");
			}

			if (($iOffset+$iPageSize) < $iCnt)
			{
				$sContent = metaRefresh($oReq->get('type'), $iPage, $iOffset, $iPageSize,  $iCnt);
			}
			else
			{
				$oDb->query("OPTIMIZE TABLE `". App::getT('user') ."`");
				$oTpl->assign(array(
					'done' => true
				));
			}

			break;

		case 'groups':

			$iPageSize = $oReq->get('total');

    	    $iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('group_name') ."");

			$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

			$aRows = $oDb->getRows("
				SELECT g.id, COUNT(m.type) AS cnt
				FROM ". App::getT('group_name') ." AS g
					LEFT JOIN ". App::getT('group_main') ." AS m ON(g.id = m.type)
				GROUP BY g.name
				ORDER BY g.id ASC LIMIT ". $iOffset .",". $iPageSize ."
			");

			foreach ($aRows as $aRow)
			{
				$oDb->update(App::getT('group_name'), array('total' => $aRow['cnt']), "id = '". $aRow['id'] ."'");
			}

			if (($iOffset+$iPageSize) < $iCnt)
			{
				$sContent = metaRefresh($oReq->get('type'), $iPage, $iOffset, $iPageSize,  $iCnt);
			}
			else
			{
				$oDb->query("OPTIMIZE TABLE `". App::getT('group_name') ."`");
				$oTpl->assign(array(
					'done' => true
				));
			}

			break;

		case 'user_comments':

			$iPageSize = $oReq->get('total');

    	    $iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('user') ."");

			$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

			$aRows = $oDb->getRows("
				SELECT u.id, COUNT(f.cid) AS cnt
				FROM ". App::getT('user') ." AS u
					LEFT JOIN ". App::getT('comments') ." AS f ON(f.typeid = '2' AND f.itemid = u.id)
				GROUP BY u.user
				ORDER BY u.id ASC LIMIT ". $iOffset .",". $iPageSize ."
			");

			foreach ($aRows as $aRow)
			{
				$oDb->update(App::getT('user'), array('c_comments' => $aRow['cnt']), "id = '". $aRow['id'] ."'");
			}

			if (($iOffset+$iPageSize) < $iCnt)
			{
				$sContent = metaRefresh($oReq->get('type'), $iPage, $iOffset, $iPageSize,  $iCnt);
			}
			else
			{
				$oDb->query("OPTIMIZE TABLE `". App::getT('user') ."`");
				$oTpl->assign(array(
					'done' => true
				));
			}

			break;

		case 'blog_comments':

			$iPageSize = $oReq->get('total');

    	    $iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('journal') ."");

			$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

			$aRows = $oDb->getRows("
				SELECT u.id, COUNT(f.cid) AS cnt
				FROM ". App::getT('journal') ." AS u
					LEFT JOIN ". App::getT('comments') ." AS f ON(f.typeid = '1' AND f.itemid = u.id)
				GROUP BY u.id
				ORDER BY u.id ASC LIMIT ". $iOffset .",". $iPageSize ."
			");

			foreach ($aRows as $aRow)
			{
				$oDb->update(App::getT('journal'), array('comment' => $aRow['cnt']), "id = '". $aRow['id'] ."'");
			}

			if (($iOffset+$iPageSize) < $iCnt)
			{
				$sContent = metaRefresh($oReq->get('type'), $iPage, $iOffset, $iPageSize,  $iCnt);
			}
			else
			{
				$oDb->query("OPTIMIZE TABLE `". App::getT('journal') ."`");
				$oTpl->assign(array(
					'done' => true
				));
			}

			break;

		case 'forum_topics':

			$iPageSize = $oReq->get('total');

    	    $iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('forum') ."");

			$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

			$aRows = $oDb->getRows("
				SELECT u.forum_id, COUNT(f.id) AS cnt
				FROM ". App::getT('forum') ." AS u
					LEFT JOIN ". App::getT('forum_topic') ." AS f ON(f.type = u.forum_id)
				GROUP BY u.forum_id
				ORDER BY u.forum_id ASC LIMIT ". $iOffset .",". $iPageSize ."
			");

			foreach ($aRows as $aRow)
			{
				$aItem = $oDb->getRow("
					SELECT id, user, title, time
					FROM " . App::getT('forum_topic') . "
					WHERE type = '" . $aRow['forum_id'] . "'
					ORDER BY last_time DESC
					LIMIT 1
				");
				$oDb->update(App::getT('forum'), array
				(
					'total_topics' => $aRow['cnt'],
					'last_time' => (isset($aItem['time']) ? $aItem['time'] : ''),
					'last_user' => (isset($aItem['user']) ? $aItem['user'] : ''),
					'last_thread' => (isset($aItem['title']) ? $aItem['title'] : ''),
					'last_id' => (isset($aItem['id']) ? $aItem['id'] : ''),
					'last_url' => (isset($aItem['id']) ? $aItem['id'] : '')
				), "forum_id = '". $aRow['forum_id'] ."'");
			}

			if (($iOffset+$iPageSize) < $iCnt)
			{
				$sContent = metaRefresh($oReq->get('type'), $iPage, $iOffset, $iPageSize,  $iCnt);
			}
			else
			{
				$oDb->query("OPTIMIZE TABLE `". App::getT('forum') ."`");
				$oTpl->assign(array(
					'done' => true
				));
			}

			break;

		case 'forum_posts':

			$iPageSize = $oReq->get('total');

    	    $iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('forum_topic') ."");

    	    if (!$oReq->get('run'))
    	    {
    	    	$oDb->update(App::getT('forum'), array('total_posts' => '0'), "1");
    	    }

			$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

			$aRows = $oDb->getRows("
				SELECT u.id, u.type, COUNT(f.id) AS cnt
				FROM ". App::getT('forum_topic') ." AS u
					LEFT JOIN ". App::getT('forum_reply') ." AS f ON(f.fid = u.id)
				GROUP BY u.id
				ORDER BY u.id ASC LIMIT ". $iOffset .",". $iPageSize ."
			");

			foreach ($aRows as $aRow)
			{
				$aItem = $oDb->getRow("
					SELECT id, user, time
					FROM " . App::getT('forum_reply') . "
					WHERE fid = '" . $aRow['id'] . "'
					ORDER BY time DESC
					LIMIT 1
				");
				
				$oDb->update(App::getT('forum_topic'), array
				(
					'total_replies' => $aRow['cnt'],
					'last_time' => (isset($aItem['time']) ? $aItem['time'] : ''),
					'last_user' => (isset($aItem['user']) ? $aItem['user'] : ''),
					'last_id' => (isset($aItem['id']) ? $aItem['id'] : '')
				), "id = '". $aRow['id'] ."'");
				
				$oDb->query("UPDATE " . App::getT('forum') . " SET total_posts = total_posts+" . $aRow['cnt'] . " WHERE forum_id = '". $aRow['type'] ."'");
			}

			if (($iOffset+$iPageSize) < $iCnt)
			{
				$sContent = metaRefresh($oReq->get('type'), $iPage, $iOffset, $iPageSize,  $iCnt);
			}
			else
			{
				$oDb->query("OPTIMIZE TABLE `". App::getT('forum_topic') ."`");
				$oDb->query("OPTIMIZE TABLE `". App::getT('forum') ."`");
				$oTpl->assign(array(
					'done' => true
				));
			}

			break;

	}

	$oTpl->assign(array(
		'refresh' => (isset($sContent) ? $sContent : '')
	));
}


?>